// pages/sign/qrcode/qrcode.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  onShow: function (options){
    var that = this
    var intervalNo = setInterval(function () {
      that.getSignQrCode();
    }, 5000)
    this.setData({
      intervalNo: intervalNo
    })
  },
  onUnload:function(){
    var intervalNo = this.data.intervalNo
    clearInterval(intervalNo)
  },
  onHide:function(){
    var intervalNo = this.data.intervalNo
    clearInterval(intervalNo)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.showLoading({
      title: '加载中',
    })
    var signId = options.signId
    this.setData({
      signId: signId
    })
    var that = this
    that.getSignQrCode();
    //请求签到信息
    wx.request({
      url: app.host + '/sign/info/' + this.data.signId,
      header: {
        accessToken: app.accessToken
      },
      method: 'get',
      complete: function(e) {
        wx.hideLoading()
        console.log(e)
        if (e.statusCode == 200) {
          if (e.data.code == 0) {
            that.setData({
              signInfo: e.data.data
            })
          }
        }
      }
    })
  },

  /** 请求签到二维码 */
  getSignQrCode: function(e) {
    var that = this
    var timestamp = (new Date()).getTime();
    var signId = this.data.signId
    //构造url
    var signQrCodeUrl = app.host + "/sign/qrcode?signId=" +
      signId + "&timestamp=" + timestamp;
    var signQrCodeFile
    //下载
    wx.downloadFile({
      url: signQrCodeUrl, 
      success(res) {
        console.log
        if (res.statusCode === 200) {
          that.setData({
            signQrCodeFile: res.tempFilePath
          })
        }
      }
    })
  }


})